home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / PCCP024.ARJ / HOST.C < prev    next >
Text File  |  1992-05-17  |  8KB  |  385 lines

  1. /*    Copyright (C) 1992 Peter Edward Cann, all rights reserved.
  2.  *    MicroSoft QuickC
  3.  */
  4.  
  5. #include<stdio.h>
  6. #include<bios.h>
  7. #include<dos.h>
  8. #include<fcntl.h>
  9. #include<sys\types.h>
  10. #include<sys\stat.h>
  11. #include<signal.h>
  12. #include<process.h>
  13. #include"port.h"
  14.  
  15. sendchar(c)
  16.     unsigned char c;
  17.     {
  18.     while(!((inp(basereg+STATREG)&TXMTMASK)&&(inp(basereg+MSTATREG)&CTSMASK)))
  19.         if(kbhit())
  20.             getch();
  21.     outp(basereg, c);
  22.     }
  23.  
  24. int follow;
  25.  
  26. quit()
  27.     {
  28.     cleanup();
  29.     exit(99);
  30.     }
  31.  
  32. sendstr(str)
  33.     char *str;
  34.     {
  35.     int i;
  36.     for(i=0;str[i]!='\0';++i)
  37.         if(str[i]=='\n')
  38.             {
  39.             sendchar('\r');
  40.             sendchar('\n');
  41.             }
  42.         else
  43.             sendchar(str[i]);
  44.     }
  45.  
  46. portgets(str)
  47.     char *str;
  48.     {
  49.     int i;
  50.     i=0;
  51.     while(i<256)
  52.         {
  53.         while(follow==index)
  54.             {
  55.             if(!(inp(basereg+MSTATREG)&DCDMASK))
  56.                 return(-1);
  57.             if(kbhit())
  58.                 getch();
  59.             }
  60.         str[i]=buf[follow++];
  61.         if(str[i]=='\b')
  62.             if(i>0)
  63.                 {
  64.                 i-=2;
  65.                 sendchar('\b');
  66.                 sendchar(' ');
  67.                 sendchar('\b');
  68.                 }
  69.             else
  70.                 {
  71.                 i--;
  72.                 sendchar(0x07);
  73.                 }
  74.         else
  75.             sendchar(str[i]);
  76.         follow%=TBUFSIZ;
  77.         if((str[i]=='\r')||(str[i]=='\n'))
  78.             {
  79.             sendchar('\r');
  80.             sendchar('\n');
  81.             str[i]='\0';
  82.             break;
  83.             }
  84.         i++;
  85.         }
  86.     str[256]='\0';
  87.     return(0);
  88.     }
  89.         
  90. pwportgets(str)
  91.     char *str;
  92.     {
  93.     int i;
  94.     i=0;
  95.     while(i<256)
  96.         {
  97.         while(follow==index)
  98.             {
  99.             if(!(inp(basereg+MSTATREG)&DCDMASK))
  100.                 return(-1);
  101.             if(kbhit())
  102.                 getch();
  103.             }
  104.         str[i]=buf[follow++];
  105.         if(str[i]=='\b')
  106.             if(i>0)
  107.                 {
  108.                 i-=2;
  109.                 sendchar('\b');
  110.                 sendchar(' ');
  111.                 sendchar('\b');
  112.                 }
  113.             else
  114.                 {
  115.                 i--;
  116.                 sendchar(0x07);
  117.                 }
  118.         else
  119.             sendchar('.');
  120.         follow%=TBUFSIZ;
  121.         if((str[i]=='\r')||(str[i]=='\n'))
  122.             {
  123.             sendchar('\r');
  124.             sendchar('\n');
  125.             str[i]='\0';
  126.             break;
  127.             }
  128.         i++;
  129.         }
  130.     str[256]='\0';
  131.     return(0);
  132.     }
  133.  
  134. main(argc, argv)
  135.     int argc;
  136.     char **argv;
  137.     {
  138.     long timestamp;
  139.     int i, j, outfd, ok, c, run, result;
  140.     char str[256];
  141.     index=follow=0;
  142.     printf("Copyright (C) 1992 Peter Edward Cann, all rights reserved.\n");
  143.     if(!strcmp(getenv("REMOTE"), "YES"))
  144.         {
  145.         printf("You appear to be already logged in remotely, judging by the environment\n");
  146.         printf("variable REMOTE, so this is probably a very bad idea.\n");
  147.         printf("Are you sure you want to run HOST? (y or n) --> ");
  148.         if(getchar()!='y') /* Note getchar() and not getch()! */
  149.             {
  150.             printf("I didn't think so!\n");
  151.             exit(99);
  152.             }
  153.         else
  154.             printf("OK, you're the boss!");
  155.         }
  156.     if(argc!=4)
  157.         {
  158.         printf("USAGE: host <comnum> <bps> <password>\n");
  159.         exit(1);
  160.         }
  161.     printf("Control-C to Exit.\n");
  162.     comnum=atoi(argv[1])-1;
  163.     speed=atoi(argv[2]);
  164.     databits='8';
  165.     parity='n';
  166.     stopbits='1';
  167.     setport();
  168.     readset();
  169.     signal(SIGINT, quit);
  170.     outp(basereg+MCTLREG, 0x03); /* Set DTR, RTS, Int. En. */
  171.     while(1)
  172.         {
  173.         printf("Awaiting carrier.\n");
  174.         while(!(inp(basereg+MSTATREG)&DCDMASK))
  175.             if(kbhit())
  176.                 getch();
  177.         printf("Carrier detected.\n");
  178.         setup();
  179.         timestamp=time(NULL);
  180.         while((time(NULL)-timestamp)<3);
  181.         follow=index;
  182.         for(i=0;i<3;++i)
  183.             {
  184.             sprintf(str, "Password? --> ");
  185.             sendstr(str);
  186.             if(pwportgets(str)==-1)
  187.                 {
  188.                 run=0;
  189.                 break;
  190.                 }
  191.             else if(!strcmp(str, argv[3]))
  192.                 {
  193.                 run=1;
  194.                 break;
  195.                 }
  196.             else
  197.                 run=0;
  198.             }
  199.         while(run)
  200.             {
  201.             follow=index; /* flush input */
  202.             sendstr("\n\n     DOWNLOAD:  (1) Xmodem   (2) Xmodem CRC   (3) Xmodem CRC 1K\n\n");
  203.             sendstr("       UPLOAD:  (4) Xmodem   (5) Xmodem CRC 1K Optional\n\n");
  204.             sendstr("          (s) Shell        (q) Quit        (^R) Reload HOST\n\n             ---> ");
  205.             while(index==follow)
  206.                 {
  207.                 if(!(inp(basereg+MSTATREG)&DCDMASK))
  208.                     {
  209.                     run=0;
  210.                     break;
  211.                     }
  212.                 if(kbhit())
  213.                     getch();
  214.                 }
  215.             if(!run)
  216.                 break;
  217.             c=buf[follow++];
  218.             follow%=TBUFSIZ;
  219.             switch(c)
  220.                 {
  221.                 case 18:
  222.                     sendstr("Reload HOST\n");
  223.                     cleanup();
  224.                     execlp("host", "host", argv[1], argv[2], argv[3], NULL);
  225.                     break;
  226.                 case 'q':
  227.                 case 'Q':
  228.                     sendstr("Quit\n");
  229.                     run=0;
  230.                     break;
  231.                 case 's':
  232.                 case 'S':
  233.                     sendstr("Shell\n");
  234.                     printf("Entering command.com remote shell.\n");
  235.                     sprintf(str, "COM%d", comnum+1);
  236.                     putenv("PROMPT=REMOTE>$p$g");
  237.                     putenv("REMOTE=YES");
  238.                     spawnlp(P_WAIT, "c:\\command.com", "command.com", str, "/e:01024", NULL);
  239.                     putenv("REMOTE=NO");
  240.                     setup();
  241.                     break;
  242.                 case '1':
  243.                     sendstr("Xmodem Download.\nSource file pathname? (Blank to cancel)\n --> ");
  244.                     if((result=portgets(str))==-1)
  245.                         {
  246.                         run=0;
  247.                         break;
  248.                         }
  249.                     if(!strlen(str))
  250.                         break;
  251.                     result=spawnlp(P_WAIT, "xmodems", "xmodems", argv[1], argv[2], "1", str, NULL);
  252.                     setup();
  253.                     sendstr("Press any key to continue: --> ");
  254.                     while(follow==index)
  255.                         {
  256.                         if(!(inp(basereg+MSTATREG)&DCDMASK))
  257.                             {
  258.                             run=0;
  259.                             break;
  260.                             }
  261.                         if(kbhit())
  262.                             getch();
  263.                         }
  264.                     follow=index;
  265.                     sprintf(str, "Exit code = %d.\n", result);
  266.                     sendstr(str);
  267.                     break;
  268.                 case '2':
  269.                     sendstr("Xmodem CRC Download.\nSource file pathname? (Blank to cancel)\n --> ");
  270.                     if((result=portgets(str))==-1)
  271.                         {
  272.                         run=0;
  273.                         break;
  274.                         }
  275.                     if(!strlen(str))
  276.                         break;
  277.                     result=spawnlp(P_WAIT, "xmcrcs", "xmcrcs", argv[1], argv[2], "1", str, NULL);
  278.                     setup();
  279.                     sendstr("Press any key to continue: --> ");
  280.                     while(follow==index)
  281.                         {
  282.                         if(!(inp(basereg+MSTATREG)&DCDMASK))
  283.                             {
  284.                             run=0;
  285.                             break;
  286.                             }
  287.                         if(kbhit())
  288.                             getch();
  289.                         }
  290.                     follow=index;
  291.                     sprintf(str, "Exit code = %d.\n", result);
  292.                     sendstr(str);
  293.                     break;
  294.                 case '3':
  295.                     sendstr("Xmodem CRC 1k Download.\nSource file pathname? (Blank to cancel)\n --> ");
  296.                     if((result=portgets(str))==-1)
  297.                         {
  298.                         run=0;
  299.                         break;
  300.                         }
  301.                     if(!strlen(str))
  302.                         break;
  303.                     result=spawnlp(P_WAIT, "xmcrc1ks", "xmcrc1ks", argv[1], argv[2], "1", str, NULL);
  304.                     setup();
  305.                     sendstr("Press any key to continue: --> ");
  306.                     while(follow==index)
  307.                         {
  308.                         if(!(inp(basereg+MSTATREG)&DCDMASK))
  309.                             {
  310.                             run=0;
  311.                             break;
  312.                             }
  313.                         if(kbhit())
  314.                             getch();
  315.                         }
  316.                     follow=index;
  317.                     sprintf(str, "Exit code = %d.\n", result);
  318.                     sendstr(str);
  319.                     break;
  320.                 case '4':
  321.                     sendstr("Xmodem Upload.\nTarget file pathname? (Blank to cancel)\n --> ");
  322.                     if((result=portgets(str))==-1)
  323.                         {
  324.                         run=0;
  325.                         break;
  326.                         }
  327.                     if(!strlen(str))
  328.                         break;
  329.                     result=spawnlp(P_WAIT, "xmodemr", "xmodemr", argv[1], argv[2], "1", str, NULL);
  330.                     setup();
  331.                     sendstr("Press any key to continue: --> ");
  332.                     while(follow==index)
  333.                         {
  334.                         if(!(inp(basereg+MSTATREG)&DCDMASK))
  335.                             {
  336.                             run=0;
  337.                             break;
  338.                             }
  339.                         if(kbhit())
  340.                             getch();
  341.                         }
  342.                     follow=index;
  343.                     sprintf(str, "Exit code = %d.\n", result);
  344.                     sendstr(str);
  345.                     break;
  346.                 case '5':
  347.                     sendstr("Xmodem CRC Upload, 1k Optional.\nTarget file pathname? (Blank to cancel)\n --> ");
  348.                     if((result=portgets(str))==-1)
  349.                         {
  350.                         run=0;
  351.                         break;
  352.                         }
  353.                     if(!strlen(str))
  354.                         break;
  355.                     result=spawnlp(P_WAIT, "xmcrc1kr", "xmcrc1kr", argv[1], argv[2], "1", str, NULL);
  356.                     setup();
  357.                     sendstr("Press any key to continue: --> ");
  358.                     while(follow==index)
  359.                         {
  360.                         if(!(inp(basereg+MSTATREG)&DCDMASK))
  361.                             {
  362.                             run=0;
  363.                             break;
  364.                             }
  365.                         if(kbhit())
  366.                             getch();
  367.                         }
  368.                     follow=index;
  369.                     sprintf(str, "Exit code = %d.\n", result);
  370.                     sendstr(str);
  371.                     break;
  372.                 }
  373.             }
  374.         printf("Dropping DTR for about 5 seconds.\n");
  375.         /* Drop DTR to hang up */
  376.         outp(basereg+MCTLREG, 0x08);
  377.         timestamp=time(NULL);
  378.         while(time(NULL)-timestamp<5)
  379.             if(kbhit())
  380.                 getch();
  381.         outp(basereg+MCTLREG, 0x0b);
  382.         cleanup();
  383.         }
  384.     }
  385.